package samples.d2.lines 
{
	import adobe.utils.CustomActions;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.curves.RoundPath;
	import shapes.shapes2D.Dot;
	import shapes.shapes2D.Line;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Recursion extends BaseSample
	{
		
		private var p0:Point;
		private var p1:Point;
		private var maxDistance:int = 20;
		private var iteration:int = 1;
		private var colors:Array = [0xE5DD00, 0x8CB202, 0x008C74, 0x004C66, 0x332B40];
		
		public function Recursion() 
		{
			
			
			p0 = new Point( 200, sh / 2 );
			p1 = new Point( sw - 200, sh / 2 );
			
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseHandler );
			
			mouseHandler();
			
		}
		
		private function mouseHandler(e:MouseEvent = null ):void 
		{
			
			graphics.clear();
			
			graphics.lineStyle( 0, color );
			graphics.beginFill( color );
			graphics.drawCircle ( p0.x, p0.y, 10 );
			graphics.drawCircle ( p1.x, p1.y, 10 );
			
			graphics.lineStyle( 0, color );
			
			eclair( p0, p1, iteration );
			iteration++;
			iteration %= 8;
		}
		
		private function eclair( p0:Point, p1:Point, iteration:int ):void
		{
			iteration--;
			
			if ( iteration > 0 )
			{	
				var middle:Point = new Point();
				middle.x = ( p1.x + p0.x ) * .5;
				middle.y = ( p1.y + p0.y ) * .5 + ( (Math.random() - .5) * maxDistance ) * iteration;
				
				graphics.lineStyle( 0, 0, 0 );
				graphics.beginFill( colors[ iteration % colors.length ] );
				graphics.drawCircle( middle.x, middle.y,  iteration * 3 );
				graphics.endFill();
				
				eclair( p0, middle, iteration );
				eclair( middle, p1, iteration );
				
			}
			else
			{
				
				graphics.lineStyle( 2, 0xFFFFFF );
				graphics.moveTo( p0.x, p0.y );
				graphics.lineTo( p1.x, p1.y );
				
				//graphics.lineStyle( 0, 0, 0 );
				//graphics.beginFill( 0xFF0000 );
				//graphics.drawCircle( p1.x, p1.y, 2 );
				//graphics.endFill();
				
				
			}
		}
		
	}

}